/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogAddFrame.java
 *
 * Created on Apr 5, 2009, 10:59:45 PM
 */
package com.os;

import com.graphics.ImageIdentity;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.DebugGraphics;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.WindowConstants;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import mapgenerator.Main;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

/**
 *
 * @author Sajarwo Anggai
 */
public class OSDialogAddFrame extends JDialog implements ActionListener, TreeSelectionListener {

    private JPanel LeftPanelRender;
    public JPanel RightPanelRender;
    private JScrollPane ScrollPaneRightPanel;
    private JButton AddFrame;
    private JButton Cancel;
    private JLabel jLabel1;
    private Main comp;
    private DefaultMutableTreeNode top;
    private JScrollPane treeView;
    private JTree tree;
    private ImageIdentity currentFrame;

    public OSDialogAddFrame(Main comp) {
        this.comp = comp;
        initComponents();
    }

    private void initComponents() {

        LeftPanelRender = new JPanel();
        ScrollPaneRightPanel = new JScrollPane();
        RightPanelRender = new JPanel();
        jLabel1 = new JLabel();
        AddFrame = new JButton();
        Cancel = new JButton();


        setTitle("Browse Image Pattern");
        setLocationByPlatform(true);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        getContentPane().setLayout(new AbsoluteLayout());

        LeftPanelRender.setBackground(new Color(226, 235, 227));
        LeftPanelRender.setBorder(BorderFactory.createEtchedBorder());
        LeftPanelRender.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
        LeftPanelRender.setDoubleBuffered(false);
        LeftPanelRender.setMinimumSize(new Dimension(200, 320));
        LeftPanelRender.setPreferredSize(new Dimension(200, 400));
        LeftPanelRender.setLayout(new GridLayout());
        getContentPane().add(LeftPanelRender, new AbsoluteConstraints(0, 51, -1, -1));

        ScrollPaneRightPanel.setBackground(new Color(219, 222, 255));
        ScrollPaneRightPanel.setOpaque(false);
        ScrollPaneRightPanel.setPreferredSize(new Dimension(807, 560));

        RightPanelRender.setBackground(new Color(226, 235, 227));
        RightPanelRender.setBorder(BorderFactory.createEtchedBorder());
        RightPanelRender.setLayout(null);
        ScrollPaneRightPanel.setViewportView(RightPanelRender);
        ScrollPaneRightPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        getContentPane().add(ScrollPaneRightPanel, new AbsoluteConstraints(210, 50, 480, 350));

        jLabel1.setText("Add Frame...");
        getContentPane().add(jLabel1, new AbsoluteConstraints(10, 10, -1, -1));

        AddFrame.setText("Add Frame");
        AddFrame.addActionListener(this);
        getContentPane().add(AddFrame, new AbsoluteConstraints(380, 410, -1, -1));

        Cancel.setText("Cancel");
        Cancel.addActionListener(this);
        getContentPane().add(Cancel, new AbsoluteConstraints(480, 410, 80, -1));

        reloadAll();
        pack();
    }

    public void reloadAll() {
        DefaultMutableTreeNode leafSprite = null;
        top = new DefaultMutableTreeNode("List of Image");
        ArrayList al = comp.ipContainer.getListImagePattern();
        System.out.println("size = " + al.size());
        for (int i = 0; i < al.size(); i++) {
            ImageIdentity ii = (ImageIdentity) al.get(i);
            if (ii.isSprite()) {
                leafSprite = addNode(ii);
                top.add(leafSprite);
            }
        }
        tree = new JTree(top);
        treeView = new JScrollPane(tree);
        Dimension minimumSize = new Dimension(170, 400);
        treeView.setMinimumSize(minimumSize);
        treeView.setPreferredSize(minimumSize);
        tree.addTreeSelectionListener(this);
        LeftPanelRender.add(treeView);
    }

    public DefaultMutableTreeNode addNode(Object obj) {
        return new DefaultMutableTreeNode(obj);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == AddFrame) {
            comp.osControlPanel.setFrame(currentFrame);
            comp.osControlPanel.show();
        }
        this.dispose();
    }

    public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (node != null & !node.isRoot() & node.isLeaf()) {
            ImageIdentity ii = (ImageIdentity) node.getUserObject();
            comp.ipControlPanel.previewPattern(RightPanelRender, ii);
            currentFrame = ii;
        }
    }
}
